// mypage/myNews/myNews.js
var app=getApp();
var util=require("../../utils/util");
var my=require("../../api/my");


Page({
  /*页面的初始数据*/
  data: {
    nav_index: 1,
    num1: 0,
    num2: 0,
    info: '',
    token: '',
    show_nav: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    var token=wx.getStorageSync('token');
    that.setData({token:token})
    //
    that.get_unRead_cateNews()
    that.get_news_list(1,token)
  },
  // 切换
  check_nav:function(e){
    var that = this,nav_index=that.data.nav_index,token=that.data.token;
    var index = e.currentTarget.dataset.index;
    if(nav_index != index){
      that.setData({nav_index:index,show_nav:false})
      that.get_news_list(index,token)
    }
  },
  // 跳转
  nav_detail:function(){
    var that=this,index=that.data.nav_index;
    wx.navigateTo({
      url: '../newsDetail/newsDetail?type='+index,
    })
  },
  //
  nav_home:function(){
    wx.switchTab({
      url: '../../pages/home/home',
    })
  },
  // 未读消息数
  get_unRead_cateNews:function(){
    var that=this,token=that.data.token;
    my.get_unRead_cateNews(token).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        that.setData({num1:info.msgCount,num2:info.noticeCount})
      }
    })
  },
  // 获取最新的通知
  get_news_list:function(type,token){
    wx.showLoading({title: '加载中'})
    var that=this;
    var params={
      "type":type
    }
    my.get_news_list(params,token).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        if(info){
          info.time=info.statusTime?util.formatTime_com_time(new Date(info.statusTime)):''
          that.setData({info: info})
        }else{
          that.setData({show_nav:true})
        }
      }
    })
  }
})